package cn.com.taiji.oospub.wechat.scheduler;

import cn.com.taiji.oospub.wechat.common.wxpay.pay.Configure;
import cn.com.taiji.oospub.wechat.entity.Accesstokensave;
import cn.com.taiji.oospub.wechat.service.IAccesstokensaveService;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.IOException;

@Service
public class AccessTokenScheduler {
    @Value("${appID}")
    public String appID;
    @Value("${appsecret}")
    public String appSecret;
    @Value("${token_url}")
    public String tokenUrl;
    private String jsapiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
    public static String jsapi_ticket = "";

    @Autowired
    private IAccesstokensaveService accesstokensaveService;
    @Autowired
    private RestTemplate restTemplate;

    //
    @PostConstruct
    @Scheduled(cron = "0 59 1-23 * * ? ")
    public void getAccessToken() throws IOException {
        String requestUrl = Configure.requestUrl.replace("APPID", appID).replace("APPSECRET", appSecret);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(requestUrl);
        HttpEntity entity = httpClient.execute(httpGet).getEntity();
        JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(entity));
        String accessToken = jsonObject.getString("access_token");
        httpClient.close();
        Accesstokensave one = accesstokensaveService.query().one();
        one.setAccessToken(accessToken);
        System.out.println("获取accessToken===========" + accessToken);
        System.out.println("jsonObject===========" + jsonObject);
        if (StringUtils.hasText(tokenUrl)) {
            try {
                HttpUtil.get(tokenUrl.replace("TOKEN", accessToken), 10);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        accesstokensaveService.updateById(one);
        getJsapiTicket();
    }

    private void getJsapiTicket() {
        String accessToken = accesstokensaveService.query().one().getAccessToken();
        JSONObject result = restTemplate.getForObject(jsapiTicketUrl.replace("ACCESS_TOKEN", accessToken), JSONObject.class);
        if ("ok".equals(result.getString("errmsg"))) {
            jsapi_ticket = result.getString("ticket");
            System.out.println("获取jsapi_ticket===========" + jsapi_ticket);
        }
    }


}
